package xin.marcher.module.price.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import xin.marcher.framework.common.util.data.DataCuttingUtil;
import xin.marcher.module.common.constants.construction.ProductConstants;
import xin.marcher.module.common.enums.DelFlagEnum;
import xin.marcher.module.common.enums.inventory.InventoryExceptionCode;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.price.converter.AuditContentConfigConverter;
import xin.marcher.module.price.domain.dto.AuditContentConfigDTO;
import xin.marcher.module.price.domain.entity.AuditContentConfigDO;
import xin.marcher.module.price.domain.request.AuditContentConfigRequest;
import xin.marcher.module.price.mapper.AuditContentConfigMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Repository
public class AuditRepository {

    @Resource
    private AuditContentConfigMapper auditContentConfigMapper;

    @Resource
    private AuditContentConfigConverter auditContentConfigConverter;

    /**
     * 创建审核内容配置
     *
     * @param request
     */
    public void saveAuditContentConfig(AuditContentConfigRequest request) {
        AuditContentConfigDO auditContentConfigDO = auditContentConfigConverter.requestToEntity(request);
        auditContentConfigDO.initCommon();
        int count = auditContentConfigMapper.insert(auditContentConfigDO);
        if (count <= 0) {
            throw new BaseBizException(InventoryExceptionCode.INVENTORY_SQL);
        }
    }

    /**
     * 删除审核内容配置
     *
     * @param customIdList
     */
    public void removeAuditContentConfig(List<String> customIdList) {
        LambdaUpdateWrapper<AuditContentConfigDO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(CollectionUtils.isNotEmpty(customIdList), AuditContentConfigDO::getCustomId, customIdList);
        updateWrapper.set(AuditContentConfigDO::getDelFlag, DelFlagEnum.DISABLED.getCode());
        int count = auditContentConfigMapper.update(null, updateWrapper);
        if (count <= 0) {
            throw new BaseBizException(InventoryExceptionCode.INVENTORY_SQL);
        }
    }

    /**
     * 查询审核内容配置
     *
     * @param customIdList
     * @return
     */
    public List<AuditContentConfigDTO> queryAuditContentConfig(List<String> customIdList) {
        List<AuditContentConfigDTO> auditContentConfigList = new ArrayList<>();
        // 一次最大查询200个数据，多个分页查询,这里做数据切割
        List<List<String>> splitList = DataCuttingUtil.dataCuttingString(customIdList, ProductConstants.QUERY_ITEM_MAX_COUNT);
        for (List<String> auditIds : splitList) {
            LambdaQueryWrapper<AuditContentConfigDO> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(AuditContentConfigDO::getCustomId, auditIds);
            queryWrapper.eq(AuditContentConfigDO::getDelFlag, DelFlagEnum.EFFECTIVE.getCode());
            // 分批次查询
            List<AuditContentConfigDO> stageList = auditContentConfigMapper.selectList(queryWrapper);
            if (!CollectionUtils.isEmpty(stageList)) {
                auditContentConfigList.addAll(auditContentConfigConverter.listEntityToDTO(stageList));
            }
        }
        return auditContentConfigList;
    }

}
